package com.phalanxim.dao.impl;

import com.phalanxim.dao.BalanceHistoryDao;
import com.phalanxim.service.BalanceHistory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Repository("balanceHistoryDao")
public class BalanceHistoryDaoImpl implements BalanceHistoryDao
{
  @Autowired
  private SessionFactory sessionFactory;
  //Balance History
  @Transactional(readOnly = true)
  public List<BalanceHistory> getBalanceHistoryList()
  {
    Session session = sessionFactory.openSession(); // not part of a transaction, so we need to open a session manually
    List<BalanceHistory> history = session.createQuery("from BalanceHistory history").list();
    session.close();
    return history;
  }

  @Transactional(readOnly = true)
  public BalanceHistory findBalanceHistory(Long id)
  {
    Session session = sessionFactory.openSession();
    BalanceHistory history = (BalanceHistory) session.createCriteria(BalanceHistory.class).add(Restrictions.eq("id", id)).uniqueResult();
    session.close();
    return history;
  }

  @Transactional
  public BalanceHistory saveBalanceHistory(BalanceHistory history)
  {
    Session session = sessionFactory.getCurrentSession();
    session.save(history);
    return history;
  }

  @Transactional
  public BalanceHistory updateBalanceHistory(BalanceHistory history)
  {
    Session session = sessionFactory.getCurrentSession();
    session.update(history);
    return history;
  }

  @Transactional
  public BalanceHistory removeBalanceHistory(BalanceHistory history)
  {
    Session session = sessionFactory.getCurrentSession();
    session.delete(history);
    return history;
  }
}
